package im.zego.zimexample.ui.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


import im.zego.zimexample.MyBasicActivity;
import im.zego.zimexample.SDKManager;

import java.util.List;

import im.zego.zimexample.MyApplication;
import im.zego.zimexample.R;
import im.zego.zimexample.adapter.ChatContentAdapter;
import im.zego.zimexample.emus.ChatType;
import im.zego.zimexample.entity.MessageInfo;


public class ZIMSessionBashActivity extends MyBasicActivity {


    RecyclerView mMsgList;
    ChatContentAdapter mainAdapter = new ChatContentAdapter();
    public EditText editText;
    LinearLayoutManager linearLayoutManager;
    SwipeRefreshLayout swipeRefreshLayout;
    public TextView textView;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉窗口标题
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏部分（电池、信号等）  全屏显示
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_session);
        editText = findViewById(R.id.content);
        mMsgList = findViewById(R.id.msg_list);
        textView = findViewById(R.id.title);
        swipeRefreshLayout = findViewById(R.id.swipe_view);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setStackFromEnd(true);

        mMsgList.setLayoutManager(linearLayoutManager);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 加载更多
                onLoadMsg();
            }
        });

        mMsgList.setAdapter(mainAdapter);
        mMsgList.setItemAnimator(new DefaultItemAnimator());
        ChatType chatType = getChatType();
        if (chatType == ChatType.CHAT_TYPE_PEER) {
            findViewById(R.id.query_member).setVisibility(View.GONE);
        }


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected ChatType getChatType() {
        return ChatType.CHAT_TYPE_PEER;
    }

    public MessageInfo getStartMessage() {
        return mainAdapter.getMessage(0);
    }

    public void addMsg(MessageInfo messageInfo) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mainAdapter.add(messageInfo);
                notifyDataSetChanged();
                scrollToPositionBottom();
            }
        });
        editText.setText("");
    }

    public void addAllMsg(List<MessageInfo> list) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mainAdapter.addAll(list);
                notifyDataSetChanged();
                scrollToPositionBottom();
            }
        });
    }


    public void closeRefreshing() {
        swipeRefreshLayout.setRefreshing(false);
    }

    public void notifyDataSetChanged() {
        closeRefreshing();
        mainAdapter.notifyDataSetChanged();
    }

    public void scrollToPositionBottom() {
        mMsgList.scrollToPosition(mainAdapter.getItemCount() - 1);
    }

    public void setTitle(String title) {
        textView.setText(title);
    }

    public void sendMsg(View view) {
        String content = editText.getText().toString();
        sendMsg(content);
    }


    public void queryMember(View view) {
    }


    public void back(View view) {
        finish();
    }

    protected void sendMsg(String content) {
    }

    @Override
    protected void onStart() {
        super.onStart();
        SDKManager.sharedInstance().showDebugLog(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKManager.sharedInstance().closeDebugLog(this);
    }

    protected void onLoadMsg() {
        mMsgList.postDelayed(new Runnable() {
            @Override
            public void run() {
                closeRefreshing();
            }
        }, 5000);
    }

}
